package com.senseu.base;

import com.senseu.utils.TapFieldDecorator;
import com.senseu.utils.TapHelper;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.pagefactory.AppiumFieldDecorator;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Nullable;
import java.time.Duration;
import java.util.List;
import java.util.function.Function;

public class BasePage {

    protected Logger logger = LoggerFactory.getLogger(getClass().getSimpleName());

    // AndroidDriver ???
    protected AppiumDriver driver;
    protected TapHelper tapHelper;

    public BasePage(AppiumDriver driver) {
        this.driver = driver;
        tapHelper = new TapHelper(driver);
        PageFactory.initElements(new TapFieldDecorator(driver, tapHelper), this);
    }

    public boolean isElementPresent(By locator,By addDevice, int timeoutSeconds) {
        try {
            logger.info("判断元素是否存在");
            WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(timeoutSeconds),Duration.ofSeconds(1));
            return wait.until(ExpectedConditions.or(ExpectedConditions.presenceOfElementLocated(locator),ExpectedConditions.presenceOfElementLocated(addDevice)));
        } catch (Exception e) {
            logger.error("判断元素是否可见报错 {}", locator.toString(), e);
            return false;
        }
    }

    @Nullable
    public WebElement waitToast(By locator, int timeoutSeconds) {
        try {
            WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(timeoutSeconds));
            return wait.until(ExpectedConditions.presenceOfElementLocated(locator));
        } catch (Exception e) {
            logger.error("获取吐司对象报错 {}", locator.toString(), e);
        }
        return null;
    }

    public String getPage() {
        return ((AndroidDriver) driver).currentActivity();
    }

    public List<WebElement> getAllElements(By locator) {
        return driver.findElements(locator);
    }

    public List<WebElement> getAllTextViewElements() {
        return getAllElements(By.className("android.widget.TextView"));
    }

    public boolean isInPage(int timeoutSeconds,String... page) {
        if (page == null || page.length == 0) {
            return false;
        }
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(timeoutSeconds),Duration.ofSeconds(2));
        return wait.until(new Function<WebDriver, Boolean>() {
            @Override
            public Boolean apply(WebDriver webDriver) {
                String string = ((AndroidDriver) driver).currentActivity();
                logger.info("判断当前界面 {}", string);
                if (string != null) {
                    for (String s : page) {
                        boolean b = string.contains(s);
                        if (b) {
                            return true;
                        }
                    }
                }
                return false;
            }
        });
    }
}